package servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.Blog;
import model.BlogDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Title: findById
 * @Author 郑雨杭
 * @Package servlet
 * @Date 2024/7/29 20:12
 * @description:
 */
@WebServlet("/findById")
public class findById extends HttpServlet {
    private ObjectMapper mapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //System.out.println("findById");
        resp.setContentType("application/json;charset=utf8");
        BlogDao blogDao = new BlogDao();
        String blogId = req.getParameter("blogId");
        int id = Integer.parseInt(blogId);
        Blog blog = blogDao.findById(id);
        String blogString = mapper.writeValueAsString(blog);
        resp.getWriter().write(blogString);
    }

    /*@Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("dopost");
    }*/
}
